<!--
 * @Author: zhaozhen zack960116@163.com
 * @Date: 2024-06-17 15:25:31
 * @LastEditors: zhaozhen zack960116@163.com
 * @LastEditTime: 2024-09-04 19:58:38
 * @FilePath: \zhlx-autoform-ui\src\components\svg-icon\index.vue
-->
<template>
	<i v-if="isShowIcon" :class="props.iconClass" :style="setIconSvgStyle" />
	<svg v-else :class="svgClass" :style="setIconImgOutStyle">
    <use :xlink:href="`#icon-${props.iconClass}`" />
    <title v-if="!!title">{{title}}</title>
	</svg>
</template>

<script setup lang="ts" name="svgIcon">
import { computed } from 'vue';

// 定义父组件传过来的值
const props = defineProps({
	// 图标名称
	iconClass: {
		type: String,
	},
  // svg图标class
  className: {
    type: String
  },
	// 图标大小
	size: {
		type: Number,
		default: () => 14,
	},
	// 图标颜色
	color: {
		type: String,
	},
  // svg标题
  title: {
    type: String,
    default: ''
  },
});

// 用于判断 element plus 自带 svg 图标的显示、隐藏
const isShowIcon = computed(() => {
  return props.iconClass?.startsWith('icon-')||props.iconClass?.indexOf('iconfont') !== -1;
})
// 设置svg图标class
const svgClass = computed(() => {
  if (props.className) {
    return 'svg-icon ' + props.className
  } else {
    return 'svg-icon'
  }
})
// 设置svg图标样式
const setIconImgOutStyle = computed(() => {
  return `width: ${props.size}px;height: ${props.size}px;display: inline-block;overflow: hidden;` + (props.color ? `fill: ${props.color}` : '');
})
// 设置icon图标样式
const setIconSvgStyle = computed(() => {
	return `font-size: ${props.size}px;color: ${props.color};`;
});
</script>

<style lang="scss" scoped>
  .svg-icon {
    width: 1.1em;
    height: 1.1em;
    margin-left: 0.35em;
    margin-right: 0.35em;
    vertical-align: -0.15em;
    fill: currentColor;
    overflow: hidden;
    display: inline-block;
  }
</style>
